import API from '../../utils/api'
import common from '../../utils/common'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      info: {},
      id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id:options.id
    })
    this.getEticket(options.id)
  },
  // 去详情
  toActivityDetail (e) {
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/pages/activityDetail/activityDetail?id=' + id
    })

  },
  // 退款
  editMoney () {
    var _this= this
    wx.showModal({
      title: '',
      content: '您确定要退款吗?',
      success(res) {
        if (res.confirm) {
          console.log('确定')
          API.editMoney({orderid:_this.data.info.id}).then(res=>{
            if(res.code == 0) {
              wx.showToast({
                title: '退款成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(res=>{
                wx.switchTab({
                  url: '/pages/memberCenter/memberCenter'
                })
              },2000)
            } else {
              wx.showModal({
                title: '',
                content: res.error_msg,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  deleOrder () {
    var _this= this
    wx.showModal({
      title: '',
      content: '您确定要删除该订单吗?',
      success(res) {
        if (res.confirm) {
          console.log('确定')
          API.deleOrder({orderid:_this.data.info.id}).then(res=>{
            if(res.code == 0) {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(res=>{
                wx.switchTab({
                  url: '/pages/memberCenter/memberCenter'
                })
              },2000)
            } else {
              wx.showModal({
                title: '',
                content: res.error_msg,
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 获取电子发票信息
  getEticket (id) {
    API.eticket({id:id}).then(res=>{
      console.log(res)
      var result = res.data[0]
      result['pay_time'] = common.dateFormat(result.pay_time,2)
      console.log(result)
      if (res.code == 0) {
        this.setData({
          info:result
        })
      }
    })
  },
    // 去查看地图
    toMap (e) {
      var that = this
      console.log(that.data.info.latitude)
      var lat = Number(that.data.info.latitude)
      var lng = Number(that.data.info.longitude)
      var address = that.data.info.address
      wx.openLocation({
        latitude:lat,
        longitude:lng,
        scale: 13,
        name:address
      })
    },
    finshedActive () {
      var stauts = this.data.info.time_sign
      var stateResult
      if (stauts == 0) {
        stateResult = '活动还未开始，您确定要完成活动'
      } else if (stauts == 1){
        stateResult = '.活动还未结束，您确定要做完成活动？'
      } else if (stauts == 2) {
        stateResult = '您确定要完成活动？'
      }
      var _this= this
    wx.showModal({
      title: '',
      content: stateResult,
      success(res) {
        if (res.confirm) {
          API.finshedActive({orderid:_this.data.info.id}).then(res=>{
              if (res.code == 0) {
                wx.showToast({
                  title: '成功',
                  icon: 'success',
                  duration: 2000
                })
                setTimeout(res=>{
                  wx.switchTab({
                    url: '/pages/memberCenter/memberCenter'
                  })
                },2000)
              }
          })
        }
      }
        })
     
    },
    // 取消订单
    cancerOrder () {
      var _this= this
      wx.showModal({
        title: '',
        content: '您确定要取消订单吗?',
        success(res) {
          if (res.confirm) {
            API.cancerOrder({orderid:_this.data.info.id}).then(res=>{
                if (res.code == 0) {
                  wx.showToast({
                    title: '成功',
                    icon: 'success',
                    duration: 2000
                  })
                  setTimeout(res=>{
                    wx.switchTab({
                      url: '/pages/memberCenter/memberCenter'
                    })
                  },2000)
                }
            })
          }
        }
          })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})